package com.wenan.ui.controls.flashMaps
{
	import com.wenan.data.DataUtil;
	import com.wenan.ui.controls.flashMaps.MapEvent;
	
	import flash.display.AVM1Movie;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	
	import flashx.textLayout.container.ScrollPolicy;
	
	import mx.containers.Canvas;
	import mx.controls.SWFLoader;
	
	public class NavigatorPanel extends Canvas
	{
		private var squarePanel:MovieClip;
		private var swfLoader:SWFLoader=null;
		private var _swfPath:String;
		private var _children:Array=new Array();
		
		public function NavigatorPanel()
		{
			super();
			this.verticalScrollPolicy=ScrollPolicy.OFF;
			this.verticalScrollPolicy=ScrollPolicy.OFF;
		}

		public function get children():Array
		{
			return _children;
		}

		public function set children(value:Array):void
		{
			_children = value;
		}

		public function get swfPath():String
		{
			return _swfPath;
		}
		
		public function set swfPath(value:String):void
		{
			if(swfLoader==null)
			{
				swfLoader= new SWFLoader();
			}
			
			if(value==null)
			{
				removeAllElements();
				return;
			}
			
			_swfPath = value;
			swfLoader.addEventListener(Event.COMPLETE,swfLoadComplete);
			swfLoader.load(_swfPath); 
		}
		
		private function swfLoadComplete(e:Event):void{
			addElement(swfLoader); 
			squarePanel = swfLoader.content as MovieClip;
			
			registActions();
		}
		
		public function removeSWF():void
		{
			swfLoader.unloadAndStop();
			removeAllElements();
		}
		
		private  function getAllMovieClips():Array
		{
			var mcs:Array = [];
			
			var me:DisplayObject;
			for(var i:uint = 0; i<squarePanel.numChildren; i++) 
			{
				me = squarePanel.getChildAt(i);
				if(me is MovieClip) 
				{
					mcs.push(me); 
					if(me.name.indexOf("instance")!=0)
					{
						_children.push(me);
					}
				}
			}
			return mcs;
		}
		
		private function registActions():void 
		{
			var mc:MovieClip;
			var mcs:Array = getAllMovieClips();
			
			for(var i:uint = 0; i<mcs.length; i++) 
			{
				mc = mcs[i];
				mc.alpha = 1;
				mc.stop();
				mc.gotoAndStop(1);
				mc.buttonMode = true;
				mc.addEventListener(MouseEvent.MOUSE_OVER, mapOverHandler);
				mc.addEventListener(MouseEvent.MOUSE_OUT, mapOutHandler);
				mc.addEventListener(MouseEvent.CLICK, mapClipHandler);
			}
		}
		
		private function mapOverHandler(event:MouseEvent):void 
		{
			var mc:MovieClip = event.currentTarget as MovieClip;
			dispatchEvent(new MapEvent(MapEvent.OVER_FLOOR,mc));
		}
		
		private function mapOutHandler(event:MouseEvent):void 
		{
			var mc:MovieClip = event.currentTarget as MovieClip;
			dispatchEvent(new MapEvent(MapEvent.OUT_FLOOR,mc));
		}
		
		private function mapClipHandler(event:MouseEvent):void 
		{
			var mc:MovieClip = event.currentTarget as MovieClip;
			dispatchEvent(new MapEvent(MapEvent.ClICK_FLOOR, mc));
		}
		
	}
}